package com.vids.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vids.mapper.PersonMapper;
import com.vids.model.entity.Person;
import com.vids.model.param.*;
import com.vids.model.vo.SubImageInfoVo;
import com.vids.service.IImageBusinessService;
import com.vids.service.IPersonService;
import com.vids.utils.BeanUtilss;
import com.vids.utils.QueryUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人像 服务实现类
 * </p>
 *
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {

    public final static String type = "11";

    private final PersonMapper personMapper;

    private final IImageBusinessService imageBusinessServiceImpl;

    public Person findByPersonID(String PersonID) {
		return baseMapper.selectById(PersonID);
    }

    @Override
    public void deleteByPersonID(String PersonID) {
	baseMapper.deleteById(PersonID);
    }

    @Override
    public PersonBatchParam getPersonList(Map<String, Object> map) {
	// 无条件查询默认返回所有
	if (map == null || map.size() == 0) {
	    List<Person> resultList = personMapper.selectList(null);
	    return buildResultModel(resultList);
	}
	Map<String, Object> paramMap = QueryUtils.buildParam(map);
	List<Person> resultList = personMapper.selectByMap(paramMap);
	return buildResultModel(resultList);
    }

    private PersonBatchParam buildResultModel(List<Person> resultList) {
	if (resultList != null && resultList.size() > 0) {
		PersonBatchParam param = new PersonBatchParam();
		PersonBatchModel batchModel = new PersonBatchModel();
	    List<PersonModel> modelList = new ArrayList<>();
	    for (Person person : resultList) {
		PersonModel model = BeanUtilss.transferObjectCommon(person, PersonModel.class);
		List<SubImageInfoVo> volist = imageBusinessServiceImpl.findSubImageInfoVo(person.getPersonID(), type);
		SubImageBatchParam imageBatchParam = new SubImageBatchParam();
		imageBatchParam.setSubImageInfoObject(volist);
		model.setSubImageList(imageBatchParam);
		modelList.add(model);
	    }
	    batchModel.setPersonObject(modelList);
	    param.setPersonListObject(batchModel);
	    return param;
	}
	return null;
    }
}
